package com.joe.updemo.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import static com.joe.updemo.utils.PayContext.ALARM_POLLING_INTERVAL;


/**
 * Description:polling request
 * File: PollingUtils.java
 * Author: Administrator
 * Create: 2018/07/16 10:13
 */
public class PollingUtils {
    private int mSeconds;//system limit need above 5
    private AlarmManager manager;
    private Context mContext;
    private static PollingUtils instance;

    public static PollingUtils getInstance() {
        if (instance == null) {
            instance = new PollingUtils();
        }
        return instance;
    }

    public void initPollingService(Context context) {
        mContext = context;
        manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    }

    public void initPollingService(Context context, int seconds) {
        mSeconds = seconds;
        mContext = context;
        manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    }

    public void setPollingSeconds(int seconds) {
        mSeconds = seconds;
    }

    /**
     * start Timing Service
     *
     * @param intent Intent
     */
    public void startTimingService(Intent intent) {
        if (manager == null) {
            return;
        }
        if (mSeconds < 0) {
            mSeconds = ALARM_POLLING_INTERVAL;
        }
        PendingIntent mPendingIntent = PendingIntent.getService(mContext, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //使用AlarmManger的setRepeating方法设置定期执行的时间间隔（seconds秒）和需要执行的Service
        manager.setExact(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis() + mSeconds * 1000, mPendingIntent);
    }

    /**
     * start Timing Service
     *
     * @param intent Intent
     */
    public void startTimingService(Intent intent, int seconds) {
        if (manager == null) {
            return;
        }
        PendingIntent mPendingIntent = PendingIntent.getService(mContext, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //使用AlarmManger的setRepeating方法设置定期执行的时间间隔（seconds秒）和需要执行的Service
        manager.setExact(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis() + seconds * 1000, mPendingIntent);
    }

    /**
     * start Polling Service
     *
     * @param intent Intent
     */
    public void startPollingService(Intent intent) {
        if (manager == null) {
            return;
        }
        //触发服务的起始时间
//        long triggerAtTime = SystemClock.elapsedRealtime();
        long triggerAtTime = System.currentTimeMillis();
        PendingIntent mPendingIntent = PendingIntent.getService(mContext, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //使用AlarmManger的setRepeating方法设置定期执行的时间间隔（seconds秒）和需要执行的Service
        manager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime,
                mSeconds * 1000, mPendingIntent);
    }

    /**
     * stop Polling Service
     *
     * @param intent Intent
     */
    public void stopPollingService(Intent intent) {
        if (manager == null) {
            return;
        }
        PendingIntent mPendingIntent = PendingIntent.getService(mContext, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //取消正在执行的服务
        manager.cancel(mPendingIntent);
    }

}
